{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2bbbdc67",
   "metadata": {},
   "source": [
    "对于if可以有什么else?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c0586f5f",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'today' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-6745b23e423e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mtoday\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"Saturday\"\u001b[0m\u001b[1;33m:\u001b[0m              \u001b[1;31m# 三个单独的代码组；\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Happy!\"\u001b[0m\u001b[1;33m)\u001b[0m                   \u001b[1;31m#一个对于“if”\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mtoday\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"Sunday\"\u001b[0m\u001b[1;33m:\u001b[0m               \u001b[1;31m#另一个对应“elif\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Recover.'\u001b[0m\u001b[1;33m)\u001b[0m                  \u001b[1;31m#最后一个对应“else\"(接受所有其他条件)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'today' is not defined"
     ]
    }
   ],
   "source": [
    "if today == \"Saturday\":              # 三个单独的代码组；\n",
    "    print(\"Happy!\")                   #一个对于“if”\n",
    "elif today == \"Sunday\":               #另一个对应“elif\"\n",
    "    print('Recover.')                  #最后一个对应“else\"(接受所有其他条件)\n",
    "else:\n",
    "    print('study,study,study')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "972073c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "if today == ‘Saturday':\n",
    "    print('Party!')\n",
    "elif today == 'Sunday':\n",
    "    if condition == 'ill';             # 这个if和else是一个代码组\n",
    "        print('Recover,then rest.')\n",
    "    else:\n",
    "        print('Rest.')\n",
    "else;\n",
    "    print('study,study,study.')          #这一行代码是一个代码组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2373c2b9",
   "metadata": {},
   "source": [
    "for 循环\n",
    " \n",
    " 需求：\n",
    "   1.循环执行该代码（使用for循环）\n",
    "    right_this_minute = datetime.today().minute\n",
    "    if righ_this_minute in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "    else:\n",
    "    print(\"Not an odd minute\")\n",
    "   2.暂停执行（标准库time模块中的sleep函数可以做到）\n",
    "   3.生成一个随机数（random模块中的randint函数可以做到)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b149002d",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Hello Mum!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5ffb76c",
   "metadata": {},
   "outputs": [],
   "source": [
    "21+21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbdb51e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "ultimate_answer = 21+21\n",
    "ultimate_answer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91093be",
   "metadata": {},
   "source": [
    "迭代处理一个对象序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca083875",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in [1,2,3]:\n",
    "       print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89fb4eac",
   "metadata": {},
   "outputs": [],
   "source": [
    "for ch in \"Hi!\":\n",
    "    print(ch)         # 此处的 ch 作为循环变量名，可以随便取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6e99ed9",
   "metadata": {},
   "source": [
    "迭代指定的次数(第一个任务)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f986159",
   "metadata": {},
   "outputs": [],
   "source": [
    "for num in range(5):\n",
    "    print('Head First Rocks!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d2bc3b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().minute\n",
    "    if right_this_minute in odds:\n",
    "        print(\"This minute seems a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd minute\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "439699d5",
   "metadata": {},
   "source": [
    "让执行暂停"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0096fb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "import time\n",
    "time.sleep(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "197830e8",
   "metadata": {},
   "source": [
    "用python生成随机整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6900c1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 示例\n",
    "import random\n",
    "help(random.randint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "671e491f",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.randint(1,60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1a14ef6",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.randint(1,60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "925e3efe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "import random\n",
    "import time \n",
    "odds = [1,3,5,7,9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59]\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().minute\n",
    "    if right_this_minute in odds:\n",
    "        print(\"This minute seems a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd minute\")\n",
    "    wait_time = random.randint(1,60)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "646c36af",
   "metadata": {},
   "source": [
    "# 啤酒童谣"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7f42973",
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"bottles\"\n",
    "for beer_num in range(99, 0, -1):\n",
    "    print(beer_num, word, \"of beer on the wall.\")\n",
    "    print(beer_num, word, \"of beer.\")\n",
    "    print(\"Take one down.\")\n",
    "    print(\"Pass it around.\")\n",
    "    if beer_num == 1:\n",
    "        print(\"No more bottles of beer on the wall.\")\n",
    "    else:\n",
    "        new_num = beer_num - 1\n",
    "        if new_num == 1:\n",
    "            word = \"bottle\"\n",
    "        print(new_num, word, \"of beer on the wall.\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "048ddc71",
   "metadata": {},
   "outputs": [],
   "source": [
    "range试验"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd629418",
   "metadata": {},
   "source": [
    "第二章：列表数据\n",
    "  数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ccc4bb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "wait_time = random.randint(1, 60)\n",
    "wait_time\n",
    "# 变量会取所赋的那个值的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42487a2b",
   "metadata": {},
   "source": [
    "字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "673b37a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "word = \"bottles\"\n",
    "word"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1189d76c",
   "metadata": {},
   "source": [
    "对象\n",
    "python中所有一切都是对象，而且对象都可以赋给变量\n",
    "\n",
    "认识4个内置数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "756489e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "1. 列表：有序的可变对象集合\n",
    "2. 元组：有序的不可变对象集合\n",
    "3. 字典：无序的键/值对象集合\n",
    "4. 集合：无需的唯一对象集合"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
